package com.mm.mweather

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.content.Intent
import android.content.SharedPreferences
import android.preference.PreferenceManager
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.mm.mweather.util.HttpUtil
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException


class SplashActivity : AppCompatActivity() {

    val  SPLASH_DISPLAY_LENGHT:Long = 5000
    var bingPicImg:ImageView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        bingPicImg = findViewById<View>(R.id.bing_pic) as ImageView

        val picUrl:String = "http://guolin.tech/api/bing_pic";
        HttpUtil.sendOkHttpRequests(picUrl,object : Callback {

            override fun onFailure(call: Call?, e: IOException?) {
                e!!.printStackTrace()
            }


            override fun onResponse(call: Call?, response: Response) {
                val bingPic:String=response.body()!!.string()


                runOnUiThread{
                    Glide.with(this@SplashActivity).load(bingPic).into(bingPicImg)

                }
            }
        })

        android.os.Handler().postDelayed(Runnable {
            val mainIntent = Intent(this@SplashActivity,
                    MainActivity::class.java)
            this@SplashActivity.startActivity(mainIntent)
            this@SplashActivity.finish()
        }, SPLASH_DISPLAY_LENGHT)

    }
}
